<?
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/*

$pg_user_info

$pg_menu

$pg_content

$pg_scripts

$pg_sectionname

*/
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

$pg_content = _mss_error; // default content yaa

$pg_scripts  = include_js('jquery');
$pg_scripts .= include_js('jquery-tinymce');
$pg_scripts .= include_js('jquery-placeholder');
$pg_scripts .= include_js('common');
$pg_scripts .= include_js('menu');

$pg_scripts .= "\n<script type='text/javascript'>var basedomain = '"._basedomain."';</script>\n";

$pg_sectionname = _mss_error;

//------------------------------------------------------------------------------

$current_user = hello();

$pg_user_info = user_pad($current_user);



$section = (isset($_GET['what'])) ? preg_replace('/[^a-z]/', '', $_GET['what']) : 'bugs';

////////////////////////////////////////////////////////////////////////////////
//
// Show Users

if($section == 'users')
{
   if( is_array($current_user) && check_user_right('sections', 'view_users', $current_user['rights']) )
   {
      $filter = '';
   
      if( isset($_POST['f']) )
      {
         $filter = preg_replace('/[^a-zа-я0-9 \.\-]/iu', '', $_POST['f']);
         $filter = strlen($filter)>0 ? $filter : false;
      }
   
      $cancreate = ( is_array($current_user) && check_user_right('sections', 'edit_users', $current_user['rights']) );
   
      $users = get_users($filter);

      $pg_content = html_userlist($users, $cancreate);
  
      $pg_sectionname = _mss_userlist;

      $pg_scripts .= include_js('userlist');
   }
   else
   {
      $pg_sectionname = _mss_error;
      
      $pg_content = get_template('error_cantviewusers.html');
   }
   
}


////////////////////////////////////////////////////////////////////////////////
//
// Show One User

elseif($section == 'user')
{
   if( is_array($current_user) && ( check_user_right('sections', 'view_users', $current_user['rights']) || $current_user['login'] == $_GET['ulogin']) )
   {

      $login = preg_replace('/[^a-z0-9\.\-\_]/', '', $_GET['ulogin']);
   
   
      $canedit    = is_array($current_user) && check_user_right('sections', 'edit_users',   $current_user['rights']);
      $canremove  = is_array($current_user) && check_user_right('sections', 'remove_users', $current_user['rights']);
      $canviewall = is_array($current_user) && check_user_right('sections', 'view_users',   $current_user['rights']);


      if($login == 'n' && $canedit)       // new user. content is just the ajax-request
      {
         $pg_content = '<script>edituser(0)</script>';
      
         $pg_sectionname = _mss_createnewuser;
         
         $pg_scripts .= include_js('userpage');
      }
      elseif(check_login($login, true))   // old user
      {
         $user = get_user(0, $login);
         $pg_content = html_userpage($user, $canedit, $canviewall, $canremove);
      
         $pg_sectionname = _mss_userpage;
         
         $pg_scripts .= include_js('userpage');
      }
      else
      {
         $pg_sectionname = _mss_error;
         $pg_content = get_template('error_nouser.html');
      }
   }
   else
   {
      $pg_sectionname = _mss_error;
      $pg_content = get_template('error_nouser.html');
   }
   
}

////////////////////////////////////////////////////////////////////////////////
//
// Places View

elseif($section == 'places')
{
   $canview = ( is_array($current_user) && check_user_right('sections', 'view_places', $current_user['rights']) );
   
   if($canview)
   {
      $pg_sectionname = _mss_placelist;
      $pg_scripts .= include_js('refc');
      $pg_scripts .= include_js('jquery-tablednd');

      $canedit = ( is_array($current_user) && check_user_right('sections', 'edit_places', $current_user['rights']) );

      $pg_content = get_rfc_html($section, $canedit);
   }
   else
   {
      $pg_sectionname = _mss_error;
      $pg_content = get_template('error_cantviewplaces.html');
   }
}

////////////////////////////////////////////////////////////////////////////////
//
// Types View

elseif($section == 'types')
{
   $canview = ( is_array($current_user) && check_user_right('sections', 'view_types', $current_user['rights']) );

   if($canview)
   {
      $pg_sectionname = _mss_typeslist;
      $pg_scripts .= include_js('refc');
      $pg_scripts .= include_js('jquery-tablednd');

      $canedit = ( is_array($current_user) && check_user_right('sections', 'edit_types', $current_user['rights']) );

      $pg_content = get_rfc_html($section, $canedit);
   }
   else
   {
      $pg_sectionname = _mss_error;
      $pg_content = get_template('error_cantviewtypes.html');
   }
}

////////////////////////////////////////////////////////////////////////////////
//
// Comment Types View

elseif($section == 'ctypes')
{
   $canview = ( is_array($current_user) && check_user_right('sections', 'view_ctypes', $current_user['rights']) );

   if($canview)
   {
      $pg_sectionname = _mss_ctypeslist;
      $pg_scripts .= include_js('refc');
      $pg_scripts .= include_js('jquery-tablednd');

      $canedit = ( is_array($current_user) && check_user_right('sections', 'edit_ctypes', $current_user['rights']) );

      $pg_content = get_rfc_html($section, $canedit);
   }
   else
   {
      $pg_sectionname = _mss_error;
      $pg_content = get_template('error_cantviewctypes.html');
   }
}


////////////////////////////////////////////////////////////////////////////////
//
// Types View

elseif($section == 'levels')
{
   $canview = ( is_array($current_user) && check_user_right('sections', 'view_levels', $current_user['rights']) );

   if($canview)
   {
      $pg_sectionname = _mss_levelslist;
      $pg_scripts .= include_js('refc');
      $pg_scripts .= include_js('jquery-tablednd');

      $canedit = ( is_array($current_user) && check_user_right('sections', 'edit_levels', $current_user['rights']) );

      $pg_content = get_rfc_html($section, $canedit);
   }
   else
   {
      $pg_sectionname = _mss_error;
      $pg_content = get_template('error_cantviewtypes.html');
   }
}

////////////////////////////////////////////////////////////////////////////////
//
// Show The One

elseif($section =='bug' && isset($_GET['bid']) && $_GET['bid']>0)
{
   $bid = justdigs($_GET['bid']);
   
   $bug = is_bug($bid, false, $current_user['level']);
   
   if(isset($bug['id']))
   {
      $canview = ( is_array($current_user) && check_user_right('bugs', 'view_bugs', $current_user['rights']));
      
      if($canview)
      {
         $current_page = ( isset($_GET['page']) ) ? justdigs($_GET['page']) : 0;
         if( !($current_page>=0) ) $current_page = 0;
         
         $pg_sectionname = _mss_bugpage;
         $pg_scripts    .= include_js('bugpage');
         $pg_content     = html_bugpage($bug, $current_user, $current_page);
      }
      else
      {
         $pg_sectionname = _mss_error;
         $pg_content = get_template('error_cantviewbugs.html');
      }
   }
   else header('location: '._basedomain.'/bugs');
}

////////////////////////////////////////////////////////////////////////////////
//
// Show All

elseif($section == 'bugs')
{
   $favs = isset($_GET['fav']) ? 1 : false;
   
   $canview = is_array($current_user) && check_user_right('bugs', 'view_bugs', $current_user['rights']);
   
   if($canview)
   {
      $_x_current_page = isset($_GET['pg']) ? justdigs($_GET['pg']) : 0;
      $_x_current_page = ($_x_current_page>0) ? $_x_current_page-1 : 0 ;
      
      $ref = ( isset($_GET['r']) ) ? ( $_GET['r']=='type' ? 'type' : 'place' ) : false;
      $req = ( isset($_GET['q']) ) ? check_rfc_item($_GET['q']) : false;
      
      $rfc = ($ref && $req && strlen($req)>0) ? get_rfc_item_by_name($ref.'s', $req) : 0;
      
      $filter = ($ref && $rfc>0) ? array($ref.'_id' => $rfc['id']) : false;

      $pg_sectionname = $favs ? _mss_favslist : _mss_bugslist;
      $pg_scripts    .= include_js('bugslist');
      $pg_content     = html_bugslist(false, $filter, false, $current_user['level'], $favs);
   }
   else
   {
      $pg_sectionname = _mss_error;
      $pg_content = get_template('error_cantviewbugs.html');
   }
}

////////////////////////////////////////////////////////////////////////////////
//
// Add The One

elseif($section == 'addnewbug' && is_array($current_user))
{
   $canadd = check_user_right('bugs', 'add_bug', $current_user['rights']);
   
   if($canadd)
   {
      $pg_sectionname = _mss_newbug;
      $pg_scripts    .= include_js('bugform');
      $pg_content     = html_bugform($current_user);
   }
   else
   {
      $pg_sectionname = _mss_error;
      $pg_content = get_template('error_cantaddbug.html');
   }
}



////////////////////////////////////////////////////////////////////////////////
//
// Edit The One

elseif($section == 'editbug' && isset($_GET['bid']) && $_GET['bid']>0 && is_array($current_user))
{
   $canedit = check_user_right('bugs', 'edit_other', $current_user['rights']);
   
   $bid = justdigs($_GET['bid']);

   $bug = get_bug($bid, $current_user['level']);
   
   if(isset($bug['id']) && $bug['id'] == $bid)
   {
   
      if(!$canedit) $canedit = ( $bug['user_id'] == $current_user['id'] && check_user_right('bugs', 'edit_own', $current_user['rights']) ) ;

      if($canedit)
      {
         $pg_sectionname = _mss_editbug;
         $pg_scripts    .= include_js('bugform');
         $pg_content     = html_bugform($current_user, $bid, $bug);
      }
      else
      {
         $pg_sectionname = _mss_error;
         $pg_content = get_template('error_canteditbug.html');
      }
   }
   else header('location: '._basedomain.'/bugs');
}


////////////////////////////////////////////////////////////////////////////////
//
// Menu loading

include('sys/a_pagemenu.php');
$pg_menu = page_menu($current_user);



//------------------------------------------------------------------------------




////////////////////////////////////////////////////////////////////////////////

// Lets Go!

include('sys/a_mainpage_tpl.php');

?>
